<template>
    <el-dialog title="编辑" :visible.sync="dialogFormVisible" :close-on-click-modal=this.$store.state.theme.interface.other.closeOnClickModal width="35%"  append-to-body>
        <el-form ref="form" :model="form" label-width="80px" size="small">
            <el-form-item label="分类">
                <el-cascader v-model="form.category_id" :options="categoryData" :props="materialProps"
                    @change="handleChange" clearable size="small" filterable style="width: 100%;">
                </el-cascader>
            </el-form-item>
        </el-form>

        <el-form ref="form" :model="form" :rules="rules" label-width="80px" size="small">
            <el-form-item label="图片名称" prop="name">
                <el-input v-model="form.name"></el-input>
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button size="mini" @click="dialogFormVisible = false">取 消</el-button>
            <el-button size="mini" type="primary" @click="edit()">确 定</el-button>
        </div>
    </el-dialog>
</template>
  
<script>
import { list } from '@/api/renovation/category';
import { edit } from '@/api/renovation/material';
export default {
    name: "edit",
    data() {
        return {
            categoryData: [],
            form: {
                category_id: [],
                name: ''
            },
            rules: {
                name: [
                    { required: true, message: '请输入素材名称', trigger: 'blur' }
                ]
            },
            materialProps: {
                checkStrictly: true,
                children: 'children',
                label: 'name',
                value: 'id'
            },
            dialogFormVisible: false
        }
    },
    mounted() {
        this.initCategoryData();
    },
    watch: {
        rowData(val) {
            this.form = val;
        }
    },
    methods: {
        initCategoryData() {
            list().then(res => {
                this.categoryData = res.data
            })
        },
        initData(row) {
            this.$nextTick(() => {
                this.form = JSON.parse(JSON.stringify(row))
                const arr = [];
                this.getParentId(this.categoryData, this.form.category_id, arr);
                this.form.category_id = arr.reverse();
            });
        },
        getParentId(data, categoryId, arr) {
            data.forEach((v) => {
                if (v.id === categoryId) {
                    arr.push(categoryId)
                    if (v.parent_id) {
                        this.getParentId(this.categoryData, v.parent_id, arr);
                    }
                } else {
                    if (v.children && v.children.length) {
                        this.getParentId(v.children, categoryId, arr)
                    }
                }
            });
        },
        edit() {
            this.$refs['form'].validate((valid) => {
                if (valid) {
                    let length = this.form.category_id.length;
                    this.form.category_id = length ? this.form.category_id[length - 1] : 0
                    edit(this.form).then(res => {
                        this.dialogFormVisible = !this.dialogFormVisible;
                        this.$message({
                            message: res.msg,
                            type: 'success'
                        });
                        this.$emit('updateList');
                    })
                } else {
                    console.log('error submit!');
                    return false;
                }
            });
        },
        handleChange(value) {
            this.form.category_id = value;
        }
    }
}
</script>
  
<style lang="less" scoped></style>